<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>范例</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="language.enumerations.serialization.html">« 序列化</a></li>
      <li style="float: right;"><a href="language.errors.html">错误 »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="language.enumerations.html">枚举</a></li>
    <li>范例</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="language.enumerations.examples" class="sect1">
  <h2 class="title">范例</h2>

  <p class="para">
   <div class="example" id="example-328">
    <p><strong>示例 #1 值受限的基本用法</strong></p>
    <div class="example-contents">
     <div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />enum&nbsp;SortOrder<br /></span><span style="color: #007700">{<br />&nbsp;&nbsp;&nbsp;&nbsp;case&nbsp;</span><span style="color: #0000BB">Asc</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;case&nbsp;</span><span style="color: #0000BB">Desc</span><span style="color: #007700">;<br />}<br /><br />function&nbsp;</span><span style="color: #0000BB">query</span><span style="color: #007700">(</span><span style="color: #0000BB">$fields</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$filter</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">SortOrder&nbsp;$order&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">SortOrder</span><span style="color: #007700">::</span><span style="color: #0000BB">Asc</span><span style="color: #007700">)&nbsp;{&nbsp;...&nbsp;}<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>
    由于确保 <code class="literal">$order</code> 不是 <code class="literal">SortOrder::Asc</code>
    就是 <code class="literal">SortOrder::Desc</code>，所以 <code class="literal">query()</code> 函数能安全处理。
    因为其他任意值都会导致 <span class="classname"><a href="class.typeerror.html" class="classname">TypeError</a></span>，
    所以不需要额外的错误检查。
    </p></div>
   </div>
  </p>

  <p class="para">

   <div class="example" id="example-329">
    <p><strong>示例 #2 值排他的高级用法</strong></p>

    <div class="example-contents">
     <div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />enum&nbsp;UserStatus</span><span style="color: #007700">:&nbsp;</span><span style="color: #0000BB">string<br /></span><span style="color: #007700">{<br />&nbsp;&nbsp;&nbsp;&nbsp;case&nbsp;</span><span style="color: #0000BB">Pending&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'P'</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;case&nbsp;</span><span style="color: #0000BB">Active&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'A'</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;case&nbsp;</span><span style="color: #0000BB">Suspended&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'S'</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;case&nbsp;</span><span style="color: #0000BB">CanceledByUser&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'C'</span><span style="color: #007700">;<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;function&nbsp;</span><span style="color: #0000BB">label</span><span style="color: #007700">():&nbsp;</span><span style="color: #0000BB">string<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #0000BB">match</span><span style="color: #007700">(</span><span style="color: #0000BB">$this</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;static::</span><span style="color: #0000BB">Pending&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">'Pending'</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;static::</span><span style="color: #0000BB">Active&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">'Active'</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;static::</span><span style="color: #0000BB">Suspended&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">'Suspended'</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;static::</span><span style="color: #0000BB">CanceledByUser&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">'Canceled&nbsp;by&nbsp;user'</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;};<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />}<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>


    <div class="example-contents"><p>
    这个例子中，用户的状态是 <code class="literal">UserStatus::Pending</code>、
     <code class="literal">UserStatus::Active</code>、<code class="literal">UserStatus::Suspended</code>、
     <code class="literal">UserStatus::CanceledByUser</code> 中的一个，具有独占性。
     函数可以根据 <code class="literal">UserStatus</code> 设置参数类型，仅支持这四种值。
    </p></div>

    <div class="example-contents"><p>
     所有四个值都有一个 <code class="literal">label()</code> 方法，返回了人类可读的字符串。
     它独立于等同于标量的“机器名”。
     机器名用于类似数据库字段或 HTML 选择框这样的地方。
    </p></div>

    <div class="example-contents">
     <div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">foreach&nbsp;(</span><span style="color: #0000BB">UserStatus</span><span style="color: #007700">::</span><span style="color: #0000BB">cases</span><span style="color: #007700">()&nbsp;as&nbsp;</span><span style="color: #0000BB">$case</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">printf</span><span style="color: #007700">(</span><span style="color: #DD0000">'&lt;option&nbsp;value="%s"&gt;%s&lt;/option&gt;\n'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$case</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">value</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$case</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">label</span><span style="color: #007700">());<br />}<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

   </div>
  </p>

 </div></div></div></body></html>